package org.jeecg.modules.hc.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.modules.hc.entity.HcRider;
import org.jeecg.modules.hc.entity.HcSite;
import org.jeecg.modules.hc.entity.HcSiteAddressDetails;
import org.jeecg.modules.hc.entity.HcSiteRelation;
import org.jeecg.modules.hc.mapper.HcRiderMapper;
import org.jeecg.modules.hc.service.IHcRiderService;
import org.jeecg.modules.hc.service.IHcSiteAddressDetailsService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @Description: 骑手
 * @Author: jeecg-boot
 * @Date: 2022-12-26
 * @Version: V1.0
 */
@Service
@Slf4j
public class HcRiderServiceImpl extends ServiceImpl<HcRiderMapper, HcRider> implements IHcRiderService {

    @Autowired
    private IHcSiteAddressDetailsService hcSiteAddressDetailsService;

    public void setInformation(HcRider param) {
        List<HcSiteAddressDetails> siteAddressDetailsList = hcSiteAddressDetailsService.list(new LambdaQueryWrapper<HcSiteAddressDetails>()
                .eq(HcSiteAddressDetails::getNameRider, param.getId())
        );

        if (!CollectionUtils.isEmpty(siteAddressDetailsList)) {
            List<String> hcSiteAddressIdList = new ArrayList<>();
            List<String> hcSiteAddressNameList = new ArrayList<>();
            String supplyAddress = "";

            for (HcSiteAddressDetails hcSiteAddressDetails : siteAddressDetailsList) {
                hcSiteAddressIdList.add(hcSiteAddressDetails.getId());
                hcSiteAddressNameList.add(hcSiteAddressDetails.getAddressName());

                try {
                    JSONArray jsonArray = JSON.parseArray(hcSiteAddressDetails.getSupplyAddress());

                    for (int i = 0; i < jsonArray.size(); i++) {
                        JSONObject jsonObject = jsonArray.getJSONObject(i);
                        String name = jsonObject.getString("name");
                        String code = jsonObject.getString("code");

                        supplyAddress += name + "（" + code + "）" + "；";
                    }
                } catch (Exception e) {
                    log.error("供应地址解析错误");
                    e.printStackTrace();
                }
            }

            param.setHcSiteAddressIds(String.join(",", hcSiteAddressIdList));
            param.setHcSiteAddressNames(String.join("；", hcSiteAddressNameList));
            param.setSupplyAddress(supplyAddress);
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void add(HcRider param) {
        this.save(param);

        //遍历大厦，并且把骑手id更新进去
        if (StringUtils.isNotBlank(param.getHcSiteAddressIds())) {
            List<String> asList = Arrays.asList(param.getHcSiteAddressIds().split(","));

            for (String siteAddressId : asList) {
                HcSiteAddressDetails hcSiteAddressDetails = hcSiteAddressDetailsService.getById(siteAddressId);

                if (hcSiteAddressDetails == null) {
                    throw new JeecgBootException("大厦不存在");
                }

                hcSiteAddressDetailsService.updateById(new HcSiteAddressDetails()
                        .setId(hcSiteAddressDetails.getId())
                        .setNameRider(param.getId())
                );
            }
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void edit(HcRider param) {
        this.updateById(param);

        if (StringUtils.isNotBlank(param.getHcSiteAddressIds())) {
            //先把关联骑手id的地址数据清空
            List<HcSiteAddressDetails> siteAddressDetailsList = hcSiteAddressDetailsService.list(new LambdaQueryWrapper<HcSiteAddressDetails>()
                    .eq(HcSiteAddressDetails::getNameRider, param.getId())
            );

            for (HcSiteAddressDetails hcSiteAddressDetails : siteAddressDetailsList) {
                hcSiteAddressDetailsService.updateById(new HcSiteAddressDetails()
                        .setId(hcSiteAddressDetails.getId())
                        .setNameRider("")
                );
            }

            List<String> asList = Arrays.asList(param.getHcSiteAddressIds().split(","));

            for (String siteAddressId : asList) {
                HcSiteAddressDetails hcSiteAddressDetails = hcSiteAddressDetailsService.getById(siteAddressId);

                if (hcSiteAddressDetails == null) {
                    throw new JeecgBootException("大厦不存在");
                }

                hcSiteAddressDetailsService.updateById(new HcSiteAddressDetails()
                        .setId(hcSiteAddressDetails.getId())
                        .setNameRider(param.getId())
                );
            }
        }
    }
}
